package com.cn.ZhiKeDianShang.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.ZhiKeDianShang.AppConfig;
import com.cn.ZhiKeDianShang.MainActivity;
import com.cn.ZhiKeDianShang.R;
import com.cn.ZhiKeDianShang.base.BaseActivity;
import com.cn.ZhiKeDianShang.model.response.UserInfoBean;
import com.cn.ZhiKeDianShang.presenter.LoginUserPresenter;
import com.cn.ZhiKeDianShang.presenter.view.LoginUserView;
import com.cn.ZhiKeDianShang.util.PrefShare;
import com.cn.ZhiKeDianShang.util.ToastUtil;
import com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.HttpUtils;
import com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.result.ObjectResult;
import com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.result.Result;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/7/3.
 * do what to 2017/7/3
 */

public class LoginActivity extends BaseActivity<LoginUserView, LoginUserPresenter> implements LoginUserView {


    @BindView(R.id.tv_text)
    TextView tvText;
    @BindView(R.id.ll_acount)
    LinearLayout llAcount;
    @BindView(R.id.ll_pwd)
    LinearLayout llPwd;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.tv_regist)
    TextView tvRegist;
    @BindView(R.id.tv_forgetpwd)
    TextView tvForgetpwd;
    @BindView(R.id.rl_appbarlayout)
    RelativeLayout rlAppbarlayout;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_pwd)
    EditText etPwd;

    @Override
    protected int getNormalLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected int getErrorLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected LoginUserPresenter createPresenter() {
        return new LoginUserPresenter(this, this);
    }

    @Override
    protected void initViewsAndEvents() {
//        CrashReport.testJavaCrash();
        //不允许左滑返回
        mSwipeBackLayout.setEnableGesture(false);
    }




    /**
     * @param
     */
    private void login(String account, String password) {
//        DialogHelper
        Map<String, String> params = new HashMap<>();
        params.put("account", account);          // 账号登陆的时候需要MD5加密，服务器需求
        params.put("password", password);// 账号登陆的时候需要MD5加密，服务器需求
        HttpUtils.post().url(AppConfig.USER_LOGIN)
                .params(params)
                .build()
                .execute(new BaseCallback<LoginRegisterResult>(LoginRegisterResult.class) {
                    @Override
                    public void onResponse(ObjectResult<LoginRegisterResult> result) {
//                        DialogHelper.dismissProgressDialog();
                        Log.d("Debug", "到达请求这里---");
                        if (Result.checkSuccess(LoginActivity.this, result)) {
                            ToastUtil.showShort("登陆成功");
                            LoginRegisterResult data = result.getData();
                            PrefShare.getInstance(LoginActivity.this).saveString("token", data.getUserinfo().getToken());
                            PrefShare.getInstance(LoginActivity.this).saveString("userId", data.getUserinfo().getId() + "");
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e) {
//                        DialogHelper.dismissProgressDialog();
//                        ToastUtil.showErrorNet(LoginActivity.this);
                    }
                });
    }


    @Override
    public void onLogin(UserInfoBean resultBean) {

    }

    @Override
    public void requestFailed(String message) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @OnClick({R.id.tv_login, R.id.tv_regist, R.id.tv_forgetpwd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_login:
//                etPhone.setText("18756221912");
//                etPwd.setText("111111");
                if (TextUtils.isEmpty(etPhone.getText().toString())) {
                    ToastUtil.showShort("账号不能为空");
                    return;
                }
                if (TextUtils.isEmpty(etPwd.getText().toString())) {
                    ToastUtil.showShort("密码不能为空");
                    return;
                }
                login(etPhone.getText().toString(), etPwd.getText().toString());
                break;
            case R.id.tv_regist:
                startActivity(new Intent(LoginActivity.this, RegistActivity.class));
                break;
            case R.id.tv_forgetpwd:
                startActivity(new Intent(LoginActivity.this, ForgetPwdActivity.class));
                break;
        }
    }


    private long mExitTime;

    //按系统返回键时出发
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                Toast.makeText(this, "再按一次退出智客电商", Toast.LENGTH_SHORT).show();
                mExitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
